# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=tools
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=11.0.0.r124.ge89de847d
pkgrel=1
_commit='e89de847dd3e05bb8e46344378ce3e124f4e7d1c'
pkgdesc="MinGW-w64 tools"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-libmangle" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('SKIP')
_tools="gendef genlib genidl genpeimg widl" # genstubdll

prepare() {
  cd ${srcdir}/mingw-w64
}

pkgver() {
  cd "${srcdir}/mingw-w64"
  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

build() {
  local cur=
  for cur in ${_tools}; do
    plain "Building ${cur} ..."
    [[ -d ${srcdir}/${MSYSTEM}-${cur} ]] && rm -rf ${srcdir}/${MSYSTEM}-${cur}
    mkdir -p ${srcdir}/${MSYSTEM}-${cur} && cd ${srcdir}/${MSYSTEM}-${cur}
    ${srcdir}/mingw-w64/mingw-w64-tools/${cur}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --with-mangle=${MINGW_PREFIX} \
      --with-widl-includedir=${MINGW_PREFIX}/include
    make
  done
}

package() {
  local cur=
  for cur in ${_tools}; do
    plain "Installing ${cur} ..."
    cd ${srcdir}/${MSYSTEM}-${cur}
    make DESTDIR=${pkgdir} install
    if [[ -f "${srcdir}/mingw-w64/mingw-w64-tools/${cur}"/COPYING ]]; then
      install -Dm644 ${srcdir}/mingw-w64/mingw-w64-tools/${cur}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/mingw-w64-tools/COPYING.${cur}
    fi
  done
}
